package com.example.job.Activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.job.R;
import com.example.job.adapter.JobAdapter;
import com.example.job.db.jobdb;
import com.example.job.db.userdb;
import com.example.job.Model.JobModel;
import com.example.job.entity.UserInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CollectActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ProgressBar progressBar;
    private JobAdapter jobAdapter;
    private ImageView back;
    private BroadcastReceiver dataUpdateReceiver;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_collect);

        progressBar = findViewById(R.id.progressBar);
        recyclerView = findViewById(R.id.recyclerView);
        back = findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        registerDataUpdateReceiver();

        init();
    }

    private void registerDataUpdateReceiver() {
        dataUpdateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("com.example.job.UPDATE_DATA")) {
                    Log.d("syj", "Broadcast received, updating data");
                    init();
                }
            }
        };
        // 添加 Context.RECEIVER_NOT_EXPORTED 参数
        registerReceiver(dataUpdateReceiver, new IntentFilter("com.example.job.UPDATE_DATA"), Context.RECEIVER_NOT_EXPORTED);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(dataUpdateReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    private void init() {
        progressBar.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        // 获取当前用户信息
        UserInfo userInfo = UserInfo.getFreshUserInfo(this);
        if (userInfo.getJob_id() == null) {
            Toast.makeText(this, "没有收藏的职位", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
            return;
        }

        // 根据 user_id 获取 job_id 列表
        String jobIds = userInfo.getJob_id();
        List<String> jobIdList = new ArrayList<>();
        if (jobIds != null && !jobIds.isEmpty()) {
            String[] ids = jobIds.split(",");
            for (String id : ids) {
                if (!id.trim().isEmpty()) {
                    jobIdList.add(id.trim());
                    System.out.println("job_id: " + id.trim());
                }
            }
        }

        // 查询 jobdb 获取职位信息
        jobdb dbHelper = new jobdb(this);
        List<JobModel> jobModels = new ArrayList<>();
        for (String jobId : jobIdList) {
            JobModel job = dbHelper.getJobById(Integer.parseInt(jobId));
            if (job != null) {
                jobModels.add(job);
            }
        }

        // 设置适配器
        jobAdapter = new JobAdapter(jobModels);
        recyclerView.setAdapter(jobAdapter);
        progressBar.setVisibility(View.GONE);
    }
}
